package com.ruiysoft.crm.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.ruiysoft.crm.dto.CourseEx;
import com.ruiysoft.crm.entity.Course;
import com.ruiysoft.crm.mapper.CourseMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Yanghu
 * @since 2018-08-20
 */
@RestController
@RequestMapping("course")
public class CourseController {
    @Autowired
    CourseMapper courseMapper;

    @GetMapping("queryById")
    public Object queryById(Integer id) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("success", false);

        try {
            Course course = courseMapper.selectById(id);
            if ( course != null) {
                map.put("success", true);
                map.put("data", course);
            }

        } catch (Exception error) {
            map.put("errorMsg", "queryById 异常: " + error.getMessage());
        }

        return map;
    }

    @PostMapping("create")
    public Object create(@RequestBody Course course) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("success", false);

        try {
            course.setCreateTime(new Date());
            course.setUpdateTime(new Date());

            if (courseMapper.insert(course) > 0)
                map.put("success", true);

        } catch (Exception error) {
            map.put("errorMsg", "create 异常: " + error.getMessage());
        }

        return map;
    }

    @GetMapping("delete")
    public Object delete(Integer id) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("success", false);

        try {
            if (courseMapper.deleteById(id) > 0)
                map.put("success", true);

        } catch (Exception error) {
            map.put("errorMsg", "delete 异常: " + error.getMessage());
        }

        return map;
    }

    @PostMapping("edit")
    public Object edit(@RequestBody Course course) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("success", false);

        try {
            course.setUpdateTime(new Date());
            if (courseMapper.updateById(course) > 0)
                map.put("success", true);
            else
                map.put("errorMsg", "更新失败!");

        } catch (Exception error) {
            map.put("errorMsg", "create 异常: " + error.getMessage());
        }

        return map;
    }

    @Cacheable("query")
    @GetMapping("query")
    public Object query() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("success", false);

        try {
//            EntityWrapper<Course> entityWrapper = new EntityWrapper();
//            List<Course> list = courseMapper.selectPage(new Page<Course>(1, 25), entityWrapper);
            List<CourseEx> list = courseMapper.selectLeftJoinValueList();
            if(list.size() > 0) {
                map.put("success", true);
                map.put("data", list);
            }
            else
                map.put("errorMsg", "查询数据失败");

        } catch (Exception error) {
            map.put("errorMsg", "delete 异常: " + error.getMessage());
        }

        return map;
    }

    @Cacheable("queryByCourseType")
    @GetMapping("queryByCourseType")
    public Object queryByCourseType(String courseType) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("success", false);

        try {
            EntityWrapper<Course> entityWrapper = new EntityWrapper();
            entityWrapper.eq("courseType", courseType);
            entityWrapper.orderBy("sequence", true);
            List<Course> list = courseMapper.selectList(entityWrapper);
            if(list.size() > 0) {
                map.put("success", true);
                map.put("data", list);
            }
            else
                map.put("errorMsg", "查询数据失败");

        } catch (Exception error) {
            map.put("errorMsg", "delete 异常: " + error.getMessage());
        }

        return map;
    }
}
